/**
 * 桨模 编辑页
 *
 * 作者 ganjun
 * 日期 2024-11-18 22:28:43
 */
layui.use(['jquery','febs', 'form', 'validate', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#modelOars-finish'),

        fileUpload = layui.fileUpload,
        laydate = layui.laydate,
        validate = layui.validate;

    form.verify(validate);
    form.render();

    initFormValue();

    function initFormValue() {
        if (modelOarsData) {
            form.val("modelOars-finish-form", {
                "id": modelOarsData.id,
                "source": modelOarsData.source,

            });
            //新加工：显示图纸完成时间和附件模块，已有、外带不显示
            if(modelOarsData.source){
                changeSource(modelOarsData.source.toString());
            }

        }
    }

    function changeSource(sourceVal){
        if('1' === sourceVal){
            $('#fileUploadDiv').show();
            $('#drawingSubmitTimeDiv').show();
        }else if('2' === sourceVal || '3' === sourceVal){
            $('#fileUploadDiv').hide();
            $('#drawingSubmitTimeDiv').hide();
        }

    }

    form.on('submit(modelOars-finish-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'upload/file',
        type: 2,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        done: function (res) {
        },
    });

    function addOrUpdate(data) {
        var params = {};
        params.modelOars = data;
        params.fileList = getFileData();
        if(data && data.source === '1'){
            if(data.drawingSubmitTime == null || data.drawingSubmitTime == ''){
                febs.alert.warn('图纸完成时间不能为空');
                return;
            }
        }
        febs.modal.confirm("提交提示！","是否提交？",function (index){
            if (data.id && data.id.length > 0) {
                febs.postArray(ctx + 'modelOars/finish', params, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                    // febs.reloadPage();
                    $('#febs-modelOars').find('#query').trigger('click');
                });
            }
        });
    }

    function getFileList() {
        if (attachs && attachs.length>0) {
            return  attachs;
        }else {
            return  [];
        }
    }

    /**
     * 获取附件
     * @returns {*[]}
     */
    function getFileData(){
        var fileDatas = $view.find(".viewFile");
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
            }
            fileList.push(data);
        })
        return fileList;
    }

    laydate.render({
        elem: "#actuallyStartdate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    laydate.render({
        elem: "#drawingSubmitTime",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    laydate.render({
        elem: "#actuallyEnddate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });
});